package com.example.lock;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class ThreadDemo4 {

	public static void main(String[] args) {
//		testList();
//		testSet();
		testMap();
	}

	public static void testList() {
//		List<String> list = new ArrayList<String>();
//		List<String> list = new Vector<>();
//		List<String> list = Collections.synchronizedList(new ArrayList<String>());
		List<String> list = new CopyOnWriteArrayList<String>();

		for (int i = 0; i < 30; i++) {
			new Thread(() -> {
				list.add(UUID.randomUUID().toString().substring(0, 8));
				System.out.println(list);
			}, "Thread" + i).start();
		}
	}

	public static void testSet() {
//		Set<String> set = new HashSet<String>();
		Set<String> set = new CopyOnWriteArraySet<String>();

		for (int i = 0; i < 30; i++) {
			new Thread(() -> {
				set.add(UUID.randomUUID().toString().substring(0, 8));
				System.out.println(set);
			}, "Thread" + i).start();
		}
	}

	public static void testMap() {
//		Map<String, String> map = new HashMap<String, String>();
		Map<String, String> map = new ConcurrentHashMap<String, String>();

		for (int i = 0; i < 30; i++) {
			String key = String.valueOf(i);
			new Thread(() -> {
				map.put(key, UUID.randomUUID().toString().substring(0, 8));
				System.out.println(map);
			}, "Thread" + i).start();
		}
	}

}
